package cn.ncu.wechat.service.impl;

import cn.ncu.wechat.util.Result;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ncu.wechat.model.Article;
import cn.ncu.wechat.service.ArticleService;
import cn.ncu.wechat.dao.ArticleMapper;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 
* @description 针对表【article(文章表)】的数据库操作Service实现
* @createDate 2025-01-07 16:03:57
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
    implements ArticleService{
        @Override
        public Result<?> getArticleDetail(Long id) {
            if (id == null) {
                return Result.error(400, "请求参数错误");
            }
            Article article = null;
            try {
                article = getById(id);

                if (article == null) {
                return Result.error(404, "资源不存在");
                }

                return Result.success(article);

            } catch (Exception e) {
                return Result.error(500, "服务器错误");
            }
        }
        @Override
        public Result changeStatus(Long id, Integer status) {

            if (id == null || status == null || (status != 0 && status != 1)) {
                return Result.error(400, "请求参数错误");
            }
            
            try{
                Article existArticle = getById(id);
                if (existArticle == null) {
                    return Result.error(404, "文章不存在");
                }

                if (status.equals(existArticle.getStatus())) {
                    return Result.success("状态未改变");
                }
                Article article = new Article();
                article.setId(id);
                article.setStatus(status);
                article.setUpdateTime(new Date());
                boolean updated = updateById(article);
                if (!updated) {
                    return Result.error(500, "更新失败");
                }
                return Result.success();
            } catch (Exception e) {
                return Result.error(500, "服务器错误");
            }
        }
}




